package com.wwy.arithmetic.newsuanfa;

/**
 * @author 王伟羽
 * @program: arithmetictest
 * @description: 相同的树
 * @create: 2025/04/07
 */

public class Xiangtongdeshu {

    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q != null) {
            return false;
        } else if (p != null && q == null) {
            return false;
        }
        if (p.val != q.val) {
            return false;
        }
        judge(p.left, q.left);
        judge(p.right, q.right);
        return true;
    }

    static boolean judge(TreeNode p, TreeNode q) {
        if (p == null && q != null) {
            return false;
        } else if (p != null && q == null) {
            return false;
        }
        if (p.val != q.val) {
            return false;
        }
        judge(p.left, q.left);
        judge(p.right, q.right);
        return true;
    }
}
